<?php
/*
 * Tạo Support Product widget
* */
class Bottombox_news extends WP_Widget
{
	
	//Khởi tạo contructor của 1 lớp
	function Bottombox_news()
	{
		parent::WP_Widget('Bottombox_news_Widget',
				esc_attr(__('Footer Tin tức/Dự án',THMENAME)),
				array('description' => esc_attr(__('Thả vào Box 1,Box 2,Box 3 footer.',THEMENAME))));

	}

	function widget( $args, $instance ) // hien thi noi dung widget
	{
		global $currency;
		extract($args);
		$title = apply_filters( 'widget_title',
				empty($instance['title']) ? '' : $instance['title'],
				$instance, $this->id_base);

		$showposts = apply_filters( 'widget_text',
				$instance['showposts'], $instance );

		$selected = apply_filters( 'widget_text',
				$instance['selected'], $instance );
		$todisplay = apply_filters( 'widget_text',
				$instance['todisplay'], $instance );

		?>
		 <div class="moduletable">
					<h3><?php echo $title;?></h3>
					<div class="ja-sidenews-list clearfix">
					
					<?php 
					$duanhot = get_option("sanphamtrangchu_".$selected);
					if($duanhot)
					{
						//$duanhot = implode(',', $duanhot);
						foreach ($duanhot as $value) 
						{
							$mpost = get_post($value);
							
							?>
							<div class="ja-slidenews-item">		
								<a class="ja-title" href="<?php echo get_permalink($value);?>" title="<?php echo $mpost->post_title;?>"><?php echo $mpost->post_title;?></a>
								  
								  <?php 
								  	$thumb = get_the_post_thumbnail($value,array(50,50));
								  
								  	if($thumb):?>
									<a href="<?php the_permalink();?>" title="<?php the_title();?>">
									<?php echo $thumb;?>				 
									</a>
						    		<?php endif;?> 
								  
								  <?php echo db_shortDesc($mpost->post_excerpt, 150);?>		  			
								            
					  		</div>
							<?php 
						}
					}	
					else
					{
						echo 'Chưa có bài viết';
					}	
					?>
  				 </div>		
 			</div>			
		<?php		
	}

	function update( $new_instance, $old_instance ) 
	{
		$instance = $old_instance;
				
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['showposts'] = strip_tags($new_instance['showposts']);		
		$instance['selected'] = strip_tags($new_instance['selected']);	
		$instance['todisplay'] = strip_tags($new_instance['todisplay']);
			
		return $instance;
	}

	function form( $instance ) 
	{
		$instance = wp_parse_args( (array) $instance, 
			array( 'title' => '','todisplay' => '', 'showposts' => '','selected'=>'' ) );
		$title = strip_tags($instance['title']);
		$showposts = strip_tags($instance['showposts']);
		$todisplay = strip_tags($instance['todisplay']);
		$selected = strip_tags($instance['selected']);
?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">
				<?php echo  __('Tiêu đề:'); ?> </label>
			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" 
				name="<?php echo $this->get_field_name('title'); ?>" type="text" 
				value="<?php if($title) echo  esc_attr($title);?>" />
		</p>
	
		<p>
			<label for="<?php echo $this->get_field_id('showposts'); ?>">
				<?php _e('Showposts:'); ?> </label>
			<input class="widefat" id="<?php echo $this->get_field_id('showposts'); ?>" 
				name="<?php echo $this->get_field_name('showposts'); ?>" type="text" 
				value="<?php if($showposts) echo  esc_attr($showposts); else echo '5';?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('category'); ?>">
				<?php echo __('Loại tin:',THEMENAME); ?> </label>						
				
					<select class="widefat" id="<?php echo $this->get_field_id('selected'); ?>" 
					name="<?php echo $this->get_field_name('selected'); ?>">
					<option <?php if($selected=='footer1'){?> selected="selected"<?php }?> value="footer1"> Tin Dưới footer cột1 </option>
					<option <?php if($selected=='footer2'){?> selected="selected"<?php }?> value="footer2"> Tin Dưới footer cột2 </option>
					<option <?php if($selected=='footer3'){?> selected="selected"<?php }?> value="footer3"> Tin Dưới footer cột3 </option>
					
					</select>
						
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('todisplay'); ?>">
				<?php echo __('Chọn hiển thị:',THEMENAME); ?> </label>						
				
				<select class="widefat" id="<?php echo $this->get_field_id('todisplay'); ?>" 
					name="<?php echo $this->get_field_name('todisplay'); ?>">
					<option value="0"><?php echo __('Mặc định',THEMENAME);?></option>
					<option <?php if($todisplay=='1') echo 'selected="selected"';?> value="1"><?php echo __('Kiểu 1',THEMENAME);?></option>
					<option <?php if($todisplay=='2') echo 'selected="selected"';?> value="2"><?php echo __('Kiểu 2 không thumb',THEMENAME);?></option>
					<option <?php if($todisplay=='3') echo 'selected="selected"';?> value="3"><?php echo __('Kiểu 3 Tiêu đề',THEMENAME);?></option>
					
				</select>
				
		</p>
		
<?php
	}
}
// End Support Product widget

register_widget('Bottombox_news');

//===========================================================================================================================


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++